package org.standard.commons.tlds;

import java.util.Locale;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



public class TagOption extends TagSupport {
	
	private static final long serialVersionUID = -3166812535628021005L;

	private String optionName = null;//
	private String selectName = null;//
	private String value = null;//
	private String onchange;
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public int doEndTag() throws JspException {

		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		
		try{
			
			//Locale locale = pageContext.getRequest().getLocale(); 得到的是浏览器的语言
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			Locale locale = (Locale) request.getSession().getAttribute("locale");
			Properties props = PropertiesFileConstants.getPropertiesFileConstants(locale);
			
			String propsValue = props.get(optionName).toString(); 
			String propsValueArray [] = propsValue.split(",");
			
			StringBuffer sb = new StringBuffer();
			sb.append("<select name=\""+selectName+"\"");
			if (id != null) {
				sb.append("id = \""+id+"\"");
			}
			if (onchange != null) {
				sb.append("onchange = \""+onchange+"\"");
			}
			sb.append(">");
			for(int i = 0; i < propsValueArray.length; i++){
				String nameValueArray [] = propsValueArray[i].split(":");
				if(value != null && nameValueArray[1].trim().equals(value)){
					sb.append("<option value=\""+nameValueArray[1].trim()+"\" selected>"+nameValueArray[0].trim()+"</option>");
				}else{
					sb.append("<option value=\""+nameValueArray[1].trim()+"\">"+nameValueArray[0].trim()+"</option>");
				}
			}
			sb.append("</select>");
			JspWriter out = pageContext.getOut();
	        out.print(sb);
	    }catch (Exception e){
	        e.printStackTrace();
	    }
	    return EVAL_BODY_INCLUDE;

	}

	public void setOptionName(String optionName) {
		this.optionName = optionName;
	}

	public void setSelectName(String selectName) {
		this.selectName = selectName;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	

}
